package pl.rzarajczyk.breaktime;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import pl.rzarajczyk.breaktime.utils.SwingUtils;
import pl.rzarajczyk.breaktime.gui.TimerWindow;
import pl.rzarajczyk.breaktime.gui.SettingsWindow;
import pl.rzarajczyk.breaktime.gui.AboutWindow;
import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.IOException;
import java.net.URL;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.imageio.ImageIO;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import pl.rzarajczyk.breaktime.utils.Scheduler;

/**
 *
 * @author rzarajczyk
 */
public class Menu extends JPopupMenu implements ApplicationFrameworkAware {

    @Autowired
    private TimerWindow timerWindow;
    
    @Autowired
    private AboutWindow aboutWindow;
    
    @Autowired
    private SettingsWindow settingsWindow;
    
    @Autowired
    private Scheduler scheduler;
    
    @Autowired
    private BreakTimeSettings settings;
    
    private ScreenCorner corner;
    List<MenuItem> items;

    ApplicationFramework applicationFramework;
    
    public Menu() {
        super();
    }

    @PostConstruct
    public void open() throws IOException {
        for (MenuItem menuItem : items) {
            final MenuAction menuAction = menuItem.getAction();
            String name = menuItem.getName();
            String iconPath = menuItem.getIcon();
            URL iconUrl = Main.class.getResource(iconPath);
            Icon icon = new ImageIcon( ImageIO.read(iconUrl) );
            JMenuItem jMenuItem = new JMenuItem(name, icon);
            jMenuItem.addMouseListener(new MouseAdapter() {
                @Override
                public void mouseReleased(MouseEvent e) {
                    menuAction.action(e, Menu.this);
                }
            });
            this.add(jMenuItem);
        }
        this.setInvoker(this);
    }

    @Override
    public void setLocation(Point point) {
        boolean left = point.getX() < SwingUtils.getScreenWidth() / 2;
        boolean top = point.getY() < SwingUtils.getScreenHeight() / 2;
        if ( top && left )
            corner = ScreenCorner.TOP_LEFT;
        else if ( top && !left )
            corner = ScreenCorner.TOP_RIGHT;
        else if ( !top && left )
            corner = ScreenCorner.BOTTOM_LEFT;
        else
            corner = ScreenCorner.BOTTOM_RIGHT;
    }

    public ScreenCorner getScreenCorner() {
        return corner;
    }

    public void invokeDefaultAction(MouseEvent e) {
        MenuAction.SHOW_TIMER.action(e, this);
    }

    public AboutWindow getAboutWindow() {
        return aboutWindow;
    }

    public SettingsWindow getSettingsWindow() {
        return settingsWindow;
    }

    public void setItems(List<MenuItem> items) {
        this.items = items;
    }

    public TimerWindow getTimerWindow() {
        return timerWindow;
    }

    public Scheduler getScheduler() {
        return scheduler;
    }

    @Override
    public void setApplicationFramework(ApplicationFramework application) {
        this.applicationFramework = application;
    }

    public ApplicationFramework getApplicationFramework() {
        return applicationFramework;
    }
    
    
    
    
}
